package com.ljb.zxing_demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.myutils.BitMatrixTool;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private ImageView imageview;
    private TextView tv;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageview = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);
        et = (EditText) findViewById(R.id.et);
    }

    public void makePic(View view) {
        String str = et.getText().toString().trim();
        if (TextUtils.isEmpty(str)) {
            Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Bitmap bitmap = BitMatrixTool.init().getBitmap(str);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "二维码.png";
        BitMatrixTool.init().saveBitmap(path, bitmap);
        imageview.setImageDrawable(BitmapDrawable.createFromPath(path));
    }

    public void readPic(View view) {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "二维码.png";
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        String contents = BitMatrixTool.init().getContents(bitmap);
        tv.setText("二维码内容为:" + contents);
        Log.e(BitMatrixTool.TAG, contents);
        Log.e(BitMatrixTool.TAG, path);
    }
}
